package tw.com.msig.b2c.car.setting.web;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import tw.com.msig.b2c.car.common.web.APILibDirector;
import tw.com.msig.b2c.car.common.web.BaseAction;
import tw.com.msig.b2c.car.setting.service.SysparamService;
import tw.com.msig.b2c.car.setting.vo.SysparamVo;

public class SysparamAction extends BaseAction
{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String forwardPage = "list";

		String sActionType = request.getParameter("sActionType");

		if ("list".equals(sActionType)) {
			forwardPage = list(request, response);
		} else if ("edit".equals(sActionType)) {
			forwardPage = edit(request, response);
		} else if ("update".equals(sActionType)) {
			forwardPage = update(request, response);
		}
		return mapping.findForward(forwardPage);
	}

	private String update(HttpServletRequest request,
			HttpServletResponse response) {
		SysparamService sysparamService = (SysparamService) APILibDirector.getSpringBean("sysparamService");
		
		
		SysparamVo sysparamVo = new SysparamVo();
		sysparamVo.setId(NumberUtils.toLong(request.getParameter("id")));
		sysparamVo.setDesc(request.getParameter("desc"));
		sysparamVo.setValue(request.getParameter("value"));
		sysparamVo.setModifier((String)request.getSession().getAttribute("b2cUser"));
		sysparamVo.setModifyTime(new Timestamp(new Date().getTime()));
		
		log.debug("{}",sysparamVo);
		sysparamService.update(sysparamVo);		
		
		return edit(request,response);
		
	}


	private String edit(HttpServletRequest request, HttpServletResponse response) {
		SysparamService sysparamService = (SysparamService) APILibDirector.getSpringBean("sysparamService");
        
		Long id = NumberUtils.toLong(request.getParameter("id"));
		
		log.debug("{}",id);
		
		
		SysparamVo sysparamVo = sysparamService.getById(id);
		
		log.debug("{}",sysparamVo);
		
		request.setAttribute("sysparam", sysparamVo);
		
		
		
		
		return "edit";
	}


	public String list(HttpServletRequest request, HttpServletResponse response) {
		SysparamService sysparamService = (SysparamService) APILibDirector.getSpringBean("sysparamService");
		List<SysparamVo> result = sysparamService.getAll();
		request.setAttribute("models", result);
		return "list";
	}

}
